/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.inventario.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.Query;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import org.primefaces.model.SortOrder;
import com.isd.global.dao.BaseDAO;
import com.isd.global.excepciones.RollbackFailureException;
import com.isd.inventario.vista.ItemCatalogo;
import javax.persistence.NoResultException;


/**
 *
 * @author ctrosch
 */
@Stateless
@LocalBean
public class ListaPrecioDAO extends BaseDAO{


    public List<ItemCatalogo> getItemCatalogo(int first, int max, String sortField, SortOrder so , Map<String,String> filtro){

        try {

            String sQuery = "SELECT i FROM ItemCatalogo i ";
            sQuery += generarStringFiltro(filtro,"i", true);
            sQuery += " ORDER BY i.nrocta, i.tippro, i.artcod ASC";

            Query q = getEntityManager().createQuery(sQuery);

            if(max>=1) {
                q.setMaxResults(max);
            }
            
            return q.getResultList();

        } catch (Exception e) {

            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getItemCatalogo", e);            
            return new ArrayList<ItemCatalogo>();
        }
    }
    
    public Integer getCantidadItemCatalogo(Map<String, String> filtro){


        try {
            String sQuery = "SELECT count(i) FROM ItemCatalogo i ";
            sQuery += generarStringFiltro(filtro,"i", true);

            return queryObject(Long.class, sQuery).intValue();

        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCantidadItemCatalogo", e);
            return null;
        }
    }

    public ItemCatalogo getItemCatalogoByProducto(Map<String, String> filtro){

        try {
            String sQuery = "SELECT i FROM ItemCatalogo i " 
                    + generarStringFiltro(filtro, "i", true);
           
            Query q = getEntityManager().createQuery(sQuery);
            
            return (ItemCatalogo) q.getSingleResult();

        } catch (NoResultException nre){
            return null;
        } catch (Exception e) {
            Logger.getLogger(ListaPrecioDAO.class.getName()).log(Level.SEVERE, "getItemCatalogoByProducto", e);
            return null;
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void generarCatalogoCliente(String nroCta, boolean reintento) throws NotSupportedException, SystemException, RollbackFailureException{

        try {

            String sQuery = "EXEC GENERAR_CATALOGO_CLIENTE '"+nroCta+"'";
           
            Query q = getEntityManager().createNativeQuery(sQuery);
            int r = q.executeUpdate();
           
        
        }catch (Exception e) {
           
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(ListaPrecioDAO.class.getName()).log(Level.SEVERE, "Error al generar catálogo", ex);
            }

            if(reintento){
                generarCatalogoCliente(nroCta, false);
            }else{
                Logger.getLogger(ListaPrecioDAO.class.getName()).log(Level.SEVERE,
                        "Error al generar catálogo por segunda vez para " + nroCta ,
                        e);
            }
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void borrarCatalogoCliente(String nroCta) throws RollbackFailureException{
        try {
            
            String sQuery = "DELETE FROM ITEM_CATALOGO WHERE ITEM_CATALOGO.NROCTA = '"+nroCta+"'";
            Query q = getEntityManager().createNativeQuery(sQuery);
            int r = q.executeUpdate();
            
        } catch (Exception e) {            
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "borrarCatalogoCliente", e);            
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void limpiarCatalogo() throws RollbackFailureException{
        try {

            String sQuery = "DELETE FROM ITEM_CATALOGO WHERE ITEM_CATALOGO.NROCTA <> '999999'";
            Query q = getEntityManager().createNativeQuery(sQuery);
            int r = q.executeUpdate();
            

        } catch (Exception e) {
            
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "limpiarCatalogo", e);
        }
    }

}
